package com.tianxin.workbao.workBao.view;

import android.content.Context;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.tianxin.workbao.workBao.R;

/**
 * Created by hacker on 2017/8/1.
 */

public class ExpandView extends FrameLayout {

    private Animation mExpandAnimation;
    private Animation mCollapseAnimation;
    private boolean mIsExpand;

    public ExpandView(@NonNull Context context) {
        super(context);
    }

    public ExpandView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initExpandView();
    }

    private void initExpandView() {
        getExpandAnimation();
        getCollapseAnimation();
    }

    private Animation getCollapseAnimation() {
        if (mCollapseAnimation != null) {
            return mCollapseAnimation;
        }
        mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse);
        mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.GONE);
            }
        });
        return mCollapseAnimation;
    }

    private Animation getExpandAnimation() {
        if (mExpandAnimation != null) {
            return mExpandAnimation;
        }
        mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
        mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.VISIBLE);
            }
        });
        return mExpandAnimation;
    }

    public void collapse() {
        if (mIsExpand) {
            mIsExpand = false;
            clearAnimation();
            startAnimation(getCollapseAnimation());
        }
    }

    public void expand() {
        if (!mIsExpand) {
            mIsExpand = true;
            clearAnimation();
            startAnimation(getExpandAnimation());
        }
    }

    public boolean isExpand() {
        return mIsExpand;
    }

    public void setExpand(boolean b){
        mIsExpand = b;
    }
}
